﻿@using Nop.Admin.Models.Logistics
@using Nop.Core.Domain.Shipping
@model LogisticsRoutePlanningModel
@using Telerik.Web.Mvc.UI

@{
    //var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    //page title
    ViewBag.Title = @T("Admin.Logistics.LogisticsRoutePlanning.Title").Text;
}

<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/subsites/Administration/Content/images/ico-catalog.png")" />
        @T("Admin.Logistics.LogisticsRoutePlanning.Title") <span> : @T("Admin.Logistics.Common.Fields.PlanId")  @Model.PlanId  ( @Model.CourierName )</span>
    </div>

</div>
@using (Html.BeginForm())
{
    <div class="t-widget t-grid">

        <table class="adminContent" id="planning-table">
            <thead class="t-grid-header">
                <tr>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.Common.Fields.Item")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.Common.Fields.PackageId")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.Common.Fields.Status")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.Common.Fields.Attribute")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.Common.Fields.ScheduledPickup")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.LogisticsRoutePlanning.Fields.OriginAddress")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.Common.Fields.Addresstype")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.LogisticsRoutePlanning.Fields.Order")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.LogisticsRoutePlanning.Fields.DestinationAddress")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.Common.Fields.Addresstype")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.LogisticsRoutePlanning.Fields.Order")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.Common.Fields.PackageNote")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Logistics.Common.Fields.Reference")</span></th>
                    <th class="t-header"><span class="t-link">@T("Admin.Common.Remove")</span></th>
                </tr>
            </thead>
            <tbody>
                @for (int i = 0; i < Model.Packages.Count(); i++)
                {
                    var pkgStatus = Model.Packages[i].Status;
                    <tr class="@(i % 2 == 1 ? "t-alt" : "") package-@Model.Packages[i].PackageId"

                        @* Display oversize package with different colour and style *@
                        @if( Model.Packages[i].OverSize )
                        {
                            @:style="color:rosybrown; font-style:oblique; font-weight:700"    
                        }                        
                        >

                        <td class="item-id">@i</td>
                        <td class="item-packageid">@Html.ActionLink(Model.Packages[i].PackageId.ToString(), "PackageEdit", new { id = Model.Packages[i].PackageId }) @Html.HiddenFor(m => m.Packages[i].PackageId)</td>
                        <td class="item-status">@Model.Packages[i].Status.ToString()</td>

                        <td class="item-status" style="width:125px;">@Model.Packages[i].Attribute  </td>

                        @*<td class="item-status" style="width:125px;">
                        @if (@Model.Packages[i].ReadyTime.HasValue)
                        {
                            @Model.Packages[i].ReadyTimeStr @:- @Model.Packages[i].PickupEndTimeStr
                        }
                        </td>*@
                        
                        <td class="item-status" style="width:125px;">
                           
                              @Model.Packages[i].PickUpTimeShow
                        </td>


                        <td class="item-pickupaddress">@Model.Packages[i].OriginAddress</td>
                        <td class="item-pickupaddressorder">@Model.Packages[i].OriginType</td>

                        <td class="item-pickupaddressorder">
                            @* Decide when pickup order can be ranked/re-ranked, and when for display only *@
                            @if (pkgStatus == ShippingStatus.ReadyForPickUp)
                            {
                                var originalVal = "";
                                if (Model.Packages[i].OriginLocationOrder.HasValue)
                                {
                                    originalVal = Model.Packages[i].OriginLocationOrder.Value.ToString();
                                }
                                
                                <input type="text" value="@originalVal" name="Packages[@i].OriginLocationOrder" data-id="@Model.Packages[i].PackageId" data-type="PickUp" style="width: 50px" />
                            }
                            else
                            {
                                <span class="donePackageOrder">@Html.DisplayTextFor(m => m.Packages[i].OriginLocationOrder)</span>
                                @Html.HiddenFor(m => m.Packages[i].OriginLocationOrder)
                            }
                        </td>

                        @*
                        <td class="item-destinationaddress">@Html.DropDownListFor(m => m.Packages[i].DestinationLocationId, @Model.Packages[i].AvailableDestinationLocations, new { onchange = "onSelect(this)" })</td>
                        *@
                        <td class="item-destinationaddress">
                            <select id="Packages_@(i)__DestinationLocationId" onchange="onSelect(this)" name="Packages[@i].DestinationLocationId">
                                @foreach (var slItem in Model.Packages[i].AvailableDestinationLocations)
                                {
                                    <option value="@slItem.Value" 
                                        @if( slItem.Selected )
                                        {
                                            @:selected="selected"
                                        }
                                    >
                                        @slItem.Text
                                    </option>
                                }
                            </select>
                        </td>

                        <td class="item-destinationaddressorder">@Model.Packages[i].DestinationType</td>

                        <td class="item-destinationaddressorder">
                            @* Decide when delivery order can be ranked/re-ranked, and when for display only *@
                            @if (pkgStatus == ShippingStatus.ReturningWarehouseFailedDelivery ||
                                 pkgStatus == ShippingStatus.Delivered || 
                                 pkgStatus == ShippingStatus.FailedPickUp  )
                            {
                                <span class="donePackageOrder">@Html.DisplayTextFor(m => m.Packages[i].DestinationLocationOrder)</span>
                                @Html.HiddenFor(m => m.Packages[i].DestinationLocationOrder)
                            }
                            else
                            {
                                var destinationVal = "";
                                if (Model.Packages[i].DestinationLocationOrder.HasValue)
                                {
                                    destinationVal = Model.Packages[i].DestinationLocationOrder.Value.ToString();
                                }
                                
                                <input type="text" value="@destinationVal" name="Packages[@i].DestinationLocationOrder" data-id="@Model.Packages[i].PackageId" data-type="Delivery" style="width: 50px" />
                            }
                        </td>

                        @* TODO: Define a new class type for this! *@
                        <td class="item-status" style = "max-width: 200px; word-wrap: break-word; word-break:break-all;">
                            @Html.DisplayFor(m => m.Packages[i].LatestPackageNoteContent)
                        </td>

                        <td class="item-status" style = "max-width: 200px; word-wrap: break-word; word-break:break-all;">
                            @Html.DisplayFor(m => m.Packages[i].Reference)
                        </td>

                        @if (!(pkgStatus == ShippingStatus.ReturningWarehouse || pkgStatus == ShippingStatus.Delivering ||
                              pkgStatus == ShippingStatus.ReturningWarehouseFailedDelivery || pkgStatus == ShippingStatus.Delivered))
                        {     
                            <td>
                                @Html.HiddenFor(model => model.Packages[i].Removed)
                                @if (Model.Packages[i].Removed.HasValue)
                                {
                                    <a class="remove" href="#">Remove</a>
                                }
                            </td>
                            
                        }
                    </tr>
                }
            </tbody>
        </table>

    </div>
    
    @Html.HiddenFor(m => m.PlanId);

    <input type="submit" id="generate" class="t-button" value="Generate Route" />
    <input type="submit" name="save-continue" class="t-button" value="@T("Admin.Common.SaveContinue")" />
    <input type="button" id="autosort" class="t-button" value="Normalize Rank" />
    <input type="button" id="restore" class="t-button" value="Reset Rank" />

    
    <script type="text/javascript">
        function onSelect(o) {
            if (o) {
                var txt = $(o).parentsUntil("tbody").find('td:last-of-type').find('input');

                if (o.value.indexOf('@Model.WarehouseAddrPrefix') != -1) {
                    $(txt).attr("data-pre", txt.val());
                    $(txt).val('@Model.WarehouseOrder');
                    $(txt).attr('disabled', 'disabled');
                } else {
                    if ($(txt).attr("data-pre")) {
                        $(txt).val($(txt).attr("data-pre"));
                    }
                    else {
                        $(txt).val('');
                    }
                    $(txt).removeAttr('disabled');
                }
            }
        };

        $(".remove").click(function (event) {
            event.preventDefault();
            $(event.currentTarget).siblings("input").val('True');
            $(event.currentTarget).parent().parent().hide();
        });
    </script>
}

@if (@Model.MapContext != null)
{
    if (Model.MapContext.ShowRoute)
    {
        <div>@Html.ActionLink("Check in", "PlannedRouteList", new { id = Model.PlanId, checkInPackagesToPlan = true })</div>
        <div>@Html.ActionLink("Refresh map", "RoutePlanning", new { id = Model.PlanId, ignoreStatus = true })</div>
    }
    
    <div>
        <script type="text/javascript">
            $(document).ready(function () {
                getMap();
            })
        </script>
        <table class="stats">
            <tbody>
                <tr>
                    <td class="orderstatistics">
                        @Html.Partial("ShowMap", Model.MapContext)
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
}